<?php 
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
class Mastervideo extends Controller
{
	public function index(){
        $session = Session::get('admin');
    if (!empty($session)) {
    $v=Db::table('mastervideo')
			->alias('v')
			->join('master t','v.master_id = t.master_id')
			->select();
			 //dump($v);
			 $t=Db::table('master')
			 ->select();
	        $this->assign('mastervideo',$v); 
	        $this->assign('master',$t);
	        
         return $this->fetch();
    
}
else{
    $this->error('您没有权限!','admin/login/index');
}
		
	}
	public function delete($id){
		//先把对应的用户头像删除
		$mastervideo=Db::name("mastervideo")->find($id);
		// dump(Db::name("users")->getLastSQL());
		// dump($user);exit;
		$video=$mastervideo["mastervideo_video"];
		$file=ROOT_PATH."public".DS."static".DS."admin".DS."video".DS.$video;
		if(file_exists($file))unlink($file);

		if(Db::name("mastervideo")->delete($id))
			$this->success('删除成功');
		else $this->error('删除失败');
	}
	public function create(){
		$t=Db::table('master')
			 ->select();
	        $this->assign('master',$t);
 return $this->fetch('create');
    }
    public function add(){
       
        //dump( input('mastervideo'));
    	$file = request()->file('mastervideo_video');
  //dump($file);
    // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
        $info = $file->rule('uniqid')->validate(['ext'=>'jpg,png,gif,jpeg,mp4'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'admin'. DS . 'video');
         // ;exit;
        if($info){
                $pic=$info->getFileName();
               // dump($pic);
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }

    	//获取表单数据
    	//$pname=$_POST["pname"];

        $data=[
        	'mastervideo_name'=>input('mastervideo_name'),
        	'master_id'=>input('master'),
        	'mastervideo_intro'=>input('mastervideo_intro'),
        	'mastervideo_video'=>empty($pic)?"1.mp4":$pic
        ];
        // dump($data);
        // exit();
    	//存入数据库
    	if(DB::name('mastervideo')->insert($data)){    		
    		$this->success('添加成功','admin/mastervideo/index');

    	}else{
    		$this->error('添加失败','admin/mastervideo/create');
    	}
    }
    public function edit(){
        //获取视频类型列表
        $mastervideo_id=input('mastervideo_id');
        //dump($activity_id);
       $mastervideo=Db::name("mastervideo")->where('mastervideo_id',$mastervideo_id)->find();
        //dump($mastervideo);exit;
        $this->assign("mastervideo",$mastervideo);
        //dump($user);
        return $this->fetch('edit');
    }
  
    public function update(){

        //图片是否修改

        //获取其他的表单数据

        $mastervideo_id=input('mastervideo_id');
        //在数据库里修改
        $file = request()->file('mastervideo_video');
        //dump($uid);
    // 移动到框架应用根目录/public/uploads/ 目录下
    if($file){
        //dump($file);
        $info = $file->rule('uniqid')->validate(['ext'=>'jpg,png,gif,jpeg,mp4'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'admin'. DS . 'video');
         // ;exit;
        if($info){
                $pic=$info->getFileName();
               //dump($pic);
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }
        $data=[
            'mastervideo_name'=>input('mastervideo_name'),
            'mastervideo_intro'=>input('mastervideo_intro'),
            'mastervideo_video'=>input('mastervideo_video'),
        ];
        //dump($activity_id);
        //dump($data);exit;
        // if(!empty($pic)){
        //  if(file_exists(ROOT_PATH . 'public' . DS . 'static'. DS . 'school'.DS.$pic))
        //      unlink(ROOT_PATH . 'public' . DS . 'static'. DS . 'school'.DS.$pic2);
        // }
//dump($data);exit;
        //存入数据库
        if(DB::name('mastervideo')->where('mastervideo_id',$mastervideo_id)->update($data)){            
        
            $this->success('修改成功','admin/mastervideo/index');

        }else{
            $this->error('修改失败','admin/mastervideo/edit');
        }
    }
    public function search(){
        $content=input('content');
        $mastervideo=Db::table('mastervideo')->where('mastervideo_name','like',$content)->select();
        $this->assign("mastervideo",$mastervideo);

        return $this->fetch('index');
       // dump($sql);
    }
}
 ?>